package com.example.demo.nyxcosmetics;

import cn.hutool.core.net.URLDecoder;
import lombok.SneakyThrows;

import java.io.File;
import java.net.URL;
import java.nio.charset.StandardCharsets;

public class ParseFileNameUtils {
    private final static String folder = "C:\\Users\\blysin\\Desktop\\images\\";
    private final static File dir = new File(folder);

    @SneakyThrows
    public static String parseFileName(String imageUrl) {
        URL url = new URL(imageUrl);
        File file = new File(url.getPath());
        return file.getName();
    }

    public static File exists(String imageUrl) {
        String fileName = parseFileName(imageUrl);
        fileName = URLDecoder.decode(fileName, StandardCharsets.UTF_8);
        System.out.print(fileName);
        System.out.print(" -> ");
        File file = new File(dir, fileName);
        System.out.println(file.exists());
        return file.exists() ? file : null;
    }


    public static void main(String[] args) {
        String images = "https://www.nyxcosmetics.com/dw/image/v2/AANG_PRD/on/demandware.static/-/Sites-cpd-nyxusa-master-catalog/default/dw3bbc388a/ProductImages/2018/Lips/Suede_Matte_Lipstick/800897170653_suedemattelipstick_daintygaze_main.jpg?sw=193&sfrm=jpg&q=70,https://www.nyxcosmetics.com/dw/image/v2/AANG_PRD/on/demandware.static/-/Sites-cpd-nyxusa-master-catalog/default/dw56c80495/ProductImages/2024/EYES/BJ-RECENTLY-DECEASED-PALETTE/NYX-PMU-Makeup-Eye-Palette-BJ-COLOR-FOR-THE-RECENTLY-DECEASED-PALETTE-BJP01-0800897264802-OpenClosed.jpg,https://www.nyxcosmetics.com/dw/image/v2/AANG_PRD/on/demandware.static/-/Sites-cpd-nyxusa-master-catalog/default/dw15940f2f/ProductImages/2024/LIPS/BEETLEJIUCE-BLG/PINK/NYX-PMU-Makeup-Lip-Lip-Gloss-BEETLEJUICE-SANDWORM-SWIRLS-BUTTER-GLOSS-SWSWRL01-0800897264772-PackshotWithTexture.jpg,https://www.nyxcosmetics.com/dw/image/v2/AANG_PRD/on/demandware.static/-/Sites-cpd-nyxusa-master-catalog/default/dwa930e733/ProductImages/2022/LIPS/THIS-IS-MILKY-GLOSS/TIMG20-MilkTheCoco/NYX-PMU-Makeup-Lips-Lip-Gloss-THIS-IS-MILKY-LIP-GLOSS-TIMG20-MILK-THE-COCO-0800897225513-OpenSwatch.jpg,https://www.nyxcosmetics.com/dw/image/v2/AANG_PRD/on/demandware.static/-/Sites-cpd-nyxusa-master-catalog/default/dwa772053d/ProductImages/2024/FACE/BUTTERMELT-BLUSH/U-KNOW-BUTTA/NYX-PMU-Makeup-Face-Blush-BUTTERMELT-BLUSH-BMBL04-U-KNOW-BUTTA-0800897257606-PackshotWithTexture.jpg?sw=193&sfrm=jpg&q=70";
        String[] arr = images.split(",");
        for (String image : arr) {
            String fileName = parseFileName(image);
            System.out.print(fileName);
            System.out.print(" -> ");
            File file = new File(dir, fileName);
            System.out.println(file.exists());

        }
    }
}
